#include "MySQLClient.hpp"
#include <iostream>
using std::cout;
using std::endl;
namespace wd{

MySQLClient::MySQLClient(){
    MYSQL *pconn = mysql_init(&_conn);
    if(!pconn){
        cout << "mysqlclient init error" << endl;
    }

}

MySQLClient::~MySQLClient(){
    mysql_close(&_conn);
}

bool MySQLClient::connect(const string & host,
                          unsigned short port,
                          const string & user,
                          const string & passwd,
                          const string & db){

    //连接数据库
    MYSQL * pconn = mysql_real_connect(&_conn,
                                       host.c_str(),
                                       user.c_str(),
                                       passwd.c_str(),
                                       db.c_str(),
                                       port,
                                       nullptr,
                                       0);
    if(!pconn){
        printf("(%d,%s)\n",mysql_errno(&_conn),mysql_error(&_conn));
        return false;
    } else{
        return true;
    }


}

//执行INSERT/UPDATA/DELETE语句
bool MySQLClient::writeOperationQuery(const string & sql){
    int ret = mysql_real_query(&_conn,sql.c_str(),sql.size());
    if(ret != 0){
        printf("(%d,%s)\n",mysql_errno(&_conn),mysql_error(&_conn));
        return false;
    }else {
        printf("Query OK\n");
        return true;
    }

}

vector<vector<string>> MySQLClient::readOperationQuery(const string & sql){
    //1.先执行查询操作

    int ret = mysql_real_query(&_conn,sql.c_str(),sql.size());
    if(ret != 0){
        //查询失败
        printf("(%d,%s)\n",mysql_errno(&_conn),mysql_error(&_conn));
        return vector<vector<string>>();
    } 

    //2.查询成功后获取结果集

    vector<vector<string>> queryRes; //存放查询结果

    MYSQL_RES *res = mysql_store_result(&_conn);

    if(!res){ //结果集不存在
        printf("(%d, %s)\n", mysql_errno(&_conn), mysql_error(&_conn));
        return vector<vector<string>>();
    }

    //有结果集
    int rows = mysql_num_rows(res);
    if(rows == 0){
        return vector<vector<string>>();
    }

    queryRes.reserve(rows+1); //为vector预留空间

    int cols = mysql_num_fields(res);
    printf("rows: %d, cols: %d\n",rows, cols);

    MYSQL_FIELD * filed = nullptr;
    vector<string> fileds;

    while((filed = mysql_fetch_field(res)) != nullptr ){
        fileds.push_back(filed->name);
    }

    queryRes.push_back(std::move(fileds)); //转到容器vector<vector<string>>

    MYSQL_ROW row;
    while((row = mysql_fetch_row(res)) != nullptr){
        vector<string> tmp;
        tmp.reserve(cols);

        for(int i = 0; i < cols; ++i){
            if(row[i] != NULL){
                tmp.push_back(string(row[i]));
            }else{
                tmp.push_back(string());

            }
        }
        queryRes.push_back(tmp);
    }

    mysql_free_result(res);
    return queryRes;

}

void MySQLClient::dump(const vector<vector<string>> & res){
    cout << "result set has " << res.size() << " row(s)" << endl;
    for(auto & vec : res) {
        for(auto & filed : vec) {
            cout << filed << "\t";
        }
        cout << endl;
    }
    cout << endl;

}



};


